#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2019-08-04 12:55
# @Author   : BYF
# file      : conftest.py
# @version  :


import pytest
from lefull.conf.config import Config
from lefull.common.db import DataBase
from lefull.common.log import case_log as log
from lefull.common.excel import Excel
from lefull.common.request import Request


@pytest.fixture(scope="session")
def case_log():
    """
    日志模块，作用域为session
    :return:
    """

    def _log(case_name, url, payload, result):
        return log(case_name, url, payload, result)

    return _log


@pytest.fixture(scope="session")
def config():
    """
    基础配置模块，作用域为session
    :return:
    """
    config = Config()
    return config


@pytest.fixture(scope="session")
def excel():
    """
    Excel数据处理模块，作用域为session
    :return:
    """
    excel = Excel()
    return excel


@pytest.fixture(scope="module")
def create_session():
    """
    创建数据库会话
    :return:
    """
    db = DataBase()

    # 关闭数据库方式1
    # with db.create_session() as session:
    #     yield session

    # 关闭数据库方式2
    # session = db.create_session()
    # yield session
    # print("\n关闭数据库会话")
    # session.close()

    session = db.create_session()
    return session


@pytest.fixture(scope="module")
def close_session():
    """
    关闭数据库会话
    :return:
    """

    def _close_session(session):
        db = DataBase()
        db.close_session(session)
        return True

    return _close_session


@pytest.fixture()
def request_get():
    """
    GET请求
    :return:
    """

    def _request_get(url, headers=None, payload=None, name=None):
        request = Request()
        response = request.request_get(url, headers, payload, name)
        return response

    return _request_get


@pytest.fixture()
def request_post():
    """
    POST请求
    :return:
    """

    def _request_post(url, headers=None, payload=None, payload_type=None, name=None):
        request = Request()
        response = request.request_post(url, headers, payload, payload_type, name)
        return response

    return _request_post


# 作用域 function
@pytest.fixture(scope='function')
def fix_func():
    print('\n方法级：fix_func...')


# 作用域 class
@pytest.fixture(scope='class')
def fix_class():
    print('\n类级：fix_class...')


# 作用域 module
@pytest.fixture(scope='module')
def fix_module():
    print('\n模块级：fix_module...')


# 作用域 session
@pytest.fixture(scope='session')
def fix_session():
    print('\n会话级：fix_session...')
